// components/desensitization/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: String | Number,
    type: String, // 信息类型 name email phone idCard bankCard
    show: {
      type: Boolean,
      default: false
    }
  },

  observers: {
    'info, show': function () {
      this.handleDesensitization()
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    showText: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleDesensitization () {
      let result = this.properties.info
      if (!result) {
        result = '-'
      } else if (!this.properties.show) {
        switch (this.properties.type) {
          case 'name':
            result = this.nameEncoded(result)
            break
          case 'email': result = this.emailEncoded(result)
            break
          case 'phone': result = this.phoneEncoded(result)
            break
          case 'idCard': result = this.idCardEncoded(result)
            break
          case 'bankCard': result = this.bankCardEncoded(result)
            break
          default: result = ''
        }
      }
      this.setData({
        showText: result
      })
    },
    /**
     * 规则
     *  1: 全脱
     *  2：脱姓
     *  3：脱敏中间一个字
     *  4：脱敏中间2个字
     *  5：脱敏前二后二
     * @param name 姓名
     * @returns {*} 脱敏名称
     */
    nameEncoded (name) {
      const length = name.length
      if (length === 1) name = this.encodedUtils(0, 1, name)
      if (length === 2) name = this.encodedUtils(0, 1, name)
      if (length === 3) name = this.encodedUtils(1, 2, name)
      if (length === 4) name = this.encodedUtils(1, 3, name)
      if (length > 4) name = this.encodedUtils(2, length - 2, name, true)
      return name
    },
    /**
     * 脱敏@前两位
     * @param email
     * @returns {*}
     */
    emailEncoded (email) {
      let emailArr = email.split('@')
      let emailStr = emailArr[0]
      emailArr[0] = this.encodedUtils(emailStr.length - 2, emailStr.length, emailStr)
      return emailArr.join('@')
    },
    /**
     * 脱敏手机前三后四 座机533****
     * @param phone
     * @returns {*}
     */
    phoneEncoded (phone) {
      if (!phone) return phone
      return phone.length === 11 ? phone.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3') : phone.replace(/([\d-]{2,})([\d-]{4})/, '$1****')
    },
    /**
     * 证件号后四
     * @param idCard
     * @returns {*}
     */
    idCardEncoded (idCard) {
      idCard = this.encodedUtils(0, idCard.length - 4, idCard)
      return idCard
    },
    /**
     * 银行卡前4后四
     * @returns {*}
     * @param bankCard
     */
    bankCardEncoded (bankCard) {
      bankCard = this.encodedUtils(4, bankCard.length - 4, bankCard)
      return bankCard
    },
    /**
     * 加密工具类
     * @param start 开始加密索引
     * @param end 结束加密索引
     * @param str 加密字符传
     * @param isSpec 特殊规则判断
     * @returns {string} 加密后字符传
     */
    encodedUtils (start, end, str, isSpec = false) {
      const startStr = str.substring(0, start)
      const endStr = str.substring(end, str.length)
      let length = end - start
      if (isSpec) length = 3
      let middleStr = new Array(length).fill('*').join('')
      return startStr + middleStr + endStr
    }
  }
})
